package com.jintian.smart.platform.system.service;

import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.base.api.system.entity.HelpEntity;
import com.jintian.smart.base.api.system.form.HelpAddForm;
import com.jintian.smart.base.api.system.form.HelpQueryForm;
import com.jintian.smart.base.api.system.form.HelpRelationForm;
import com.jintian.smart.base.api.system.form.HelpUpdateForm;
import com.jintian.smart.base.api.system.view.HelpDetailVO;
import com.jintian.smart.base.api.system.view.HelpVO;
import com.jintian.smart.platform.system.mapper.HelpMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.beetl.sql.core.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台管理业务
 */
@Service
public class HelpService {
    @Resource
    private HelpMapper helpMapper;

    /**
     * 查询 帮助文档
     *
     * @param queryForm
     * @return
     */
    public PageResult<HelpVO> query(HelpQueryForm queryForm) {
        PageResult<HelpVO> page = helpMapper.query(queryForm);
        return page;
    }

    /**
     * 添加
     *
     * @param addForm
     * @return
     */
    public ResponseDTO<String> add(HelpAddForm addForm) {
        HelpEntity helpDaoEntity = SmartBeanUtil.copy(addForm, HelpEntity.class);
        this.save(helpDaoEntity, addForm.getRelationList());
        return ResponseDTO.ok();
    }


    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(HelpUpdateForm updateForm) {
        // 更新
        HelpEntity helpDaoEntity = SmartBeanUtil.copy(updateForm, HelpEntity.class);
        this.update(helpDaoEntity, updateForm.getRelationList());
        return ResponseDTO.ok();
    }


    /**
     * 删除
     *
     * @param helpDocId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long helpDocId) {
        HelpEntity helpDaoEntity = helpMapper.single(helpDocId);
        if (helpDaoEntity != null) {
            helpMapper.deleteById(helpDocId);
            helpMapper.deleteRelation(helpDocId);
        }
        return ResponseDTO.ok();
    }

    /**
     * 获取详情
     *
     * @param helpDocId
     * @return
     */
    public HelpDetailVO getDetail(Long helpDocId) {
        HelpEntity helpDaoEntity = helpMapper.single(helpDocId);
        HelpDetailVO detail = SmartBeanUtil.copy(helpDaoEntity, HelpDetailVO.class);
        if (detail != null) {
            detail.setRelationList(helpMapper.queryRelationByHelpDoc(helpDocId));
        }
        return detail;
    }

    /**
     * 获取详情
     *
     * @param relationId
     * @return
     */
    public List<HelpVO> queryHelpDocByRelationId(Long relationId) {
        return helpMapper.queryHelpDocByRelationId(relationId);
    }

    /**
     * 保存
     *
     * @param helpEntity
     * @param relationList
     */
    @Transactional(rollbackFor = Throwable.class)
    public void save(HelpEntity helpEntity, List<HelpRelationForm> relationList) {
        helpMapper.insert(helpEntity);
        Long helpDocId = helpEntity.getHelpDocId();
        // 保存关联
        if (CollectionUtils.isNotEmpty(relationList)) {
            helpMapper.insertRelation(helpDocId, relationList);
        }
    }

    /**
     * 更新
     *
     * @param helpEntity
     * @param relationList
     */
    @Transactional(rollbackFor = Throwable.class)
    public void update(HelpEntity helpEntity, List<HelpRelationForm> relationList) {
        helpMapper.updateTemplateById(helpEntity);
        Long helpDocId = helpEntity.getHelpDocId();
        // 保存关联
        if (CollectionUtils.isNotEmpty(relationList)) {
            helpMapper.deleteRelation(helpDocId);
            helpMapper.insertRelation(helpDocId, relationList);
        }
    }
}
